package com.woniuxy.rabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class LightRabbitConfig {
    // 延迟队列（15秒后消息进入死信队列）
    @Bean
    public Queue lightAutoOffDelayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", "light.exchange");
        args.put("x-dead-letter-routing-key", "light.off");
        args.put("x-message-ttl", 15000); // 15秒
        return new Queue("light.autooff.delay.queue", true, false, false, args);
    }

    // 死信队列（自动关灯队列）
    @Bean
    public Queue lightOffQueue() {
        return new Queue("light.off.queue", true);
    }

    // 交换机
    @Bean
    public DirectExchange lightExchange() {
        return new DirectExchange("light.exchange");
    }

    // 延迟队列绑定
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(lightAutoOffDelayQueue()).to(lightExchange()).with("light.autooff");
    }

    // 死信队列绑定
    @Bean
    public Binding offBinding() {
        return BindingBuilder.bind(lightOffQueue()).to(lightExchange()).with("light.off");
    }
} 